package com.timkj.zgyd.async;

import com.timkj.zgyd.dao.ZgydOrderDao;
import com.timkj.zgyd.dao.ZgydTelephoneDao;
import com.timkj.zgyd.entity.Zgyd_order_tb;
import com.timkj.zgyd.entity.Zgyd_telephone_tb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author : liji
 * @date : 2020-09-03 12:20
 */
@Component
public class AsyncService {
    private ZgydOrderDao zgydOrderDao;
    private ZgydTelephoneDao zgydTelephoneDao;

    @Autowired
    public AsyncService(ZgydOrderDao zgydOrderDao,
                        ZgydTelephoneDao zgydTelephoneDao) {
        this.zgydOrderDao = zgydOrderDao;
        this.zgydTelephoneDao = zgydTelephoneDao;
    }

    /**
     * 监听订单结束
     */
    @Async
    public void monitorOrder(Zgyd_order_tb orderTb){
        try {
            Thread.sleep(900000);
            Zgyd_order_tb orderTb1 = zgydOrderDao.SelectOrderByBh(orderTb.getZgyd_order_bh());
            if (orderTb1!=null){
                if (!orderTb1.getZgyd_order_status().equals("支付成功") &&
                        !orderTb1.getZgyd_order_status().equals("订单完成") &&
                        !orderTb1.getZgyd_order_status().equals("订单已取消")){
                    orderTb1.setZgyd_order_status("订单超时结束");
                    zgydOrderDao.UpdateOrder(orderTb1);
                    Zgyd_telephone_tb telephoneTb = new Zgyd_telephone_tb();
                    telephoneTb.setZgyd_telephone_status("可选");
                    telephoneTb.setZgyd_telephone_number(orderTb.getZgyd_order_telephone());
                    telephoneTb.setZgyd_telephone_openid(null);
                    int i =zgydTelephoneDao.UpdateTelephoneOpenid(telephoneTb);
                    int j = zgydTelephoneDao.UpdateTelephoneStatus(telephoneTb);
                    System.out.println("订单超时 " + i + " " + j);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
